package ctr_layer;

import java.util.List;

import model_layer.Supplier;
import db_layer.SupplierDB;

/*
 * The calls SupplierCtr represents the controller of the data management of the suppliers in the system.
 * It tackles information about a supplier between Database layer and model layer.
 */
public class SupplierCtr
{
	Supplier suppl;
	SupplierDB db;
	
	/*
	 * Suppliers Constructor for initialising the connection to the Database. 
	 */
	public SupplierCtr()
	{
		db=new SupplierDB();
		suppl=null;
	}
	
	/**
	 *	Method for creating a new supplier in the system.
	 *	It takes all the provided data for a supplier and creates a new entry in the database.
	 * @param name String value with the name of the Supplier.
	 * @param address String value with the address of the supplier.
	 * @param zipCode String value with the Zip Code of the supplier.
	 * @param city String value with the city of the supplier.
	 * @param phone_nr String value with the phone number of the supplier.
	 * @param email String value with the email of the supplier.
	 * @param description String value with a short description of the supplier(e.g. specific needs and requirements, more information about him, etc.)
	 * @param CVR String value with the CVR of the supplier
	 * @return a boolean value that marks if all the steps were successful.
	 */
	public boolean create_supplier(String name, String phone, String email, String address, String city, String zipCode, String CVR, String description)
	{
		Supplier newsuppl= new Supplier();
		newsuppl.setName(name);
		newsuppl.setAddress(address);
		newsuppl.setZipcode(zipCode);
		newsuppl.setCity(city);
		newsuppl.setPhone_nr(phone);
		newsuppl.setEmail(email);
		newsuppl.setDescription(description);
		newsuppl.setCvr(CVR);
		newsuppl.setDeleted(false);
		return db.insert_supplier(newsuppl);
	}
	
	/**
	 *	Method for editing/updating an existing supplier in the system.
	 *	It takes all the provided data for a supplier and updates the existing entry in the database.
	 * @param name String value with the name of the Supplier.
	 * @param address String value with the address of the supplier.
	 * @param zipCode String value with the Zip Code of the supplier.
	 * @param city String value with the city of the supplier.
	 * @param phone_nr String value with the phone number of the supplier.
	 * @param email String value with the email of the supplier.
	 * @param description String value with a short description of the supplier(e.g. specific needs and requirements, more information about him, etc.)
	 * @param CVR String value with the CVR of the supplier
	 * @return a boolean value that marks if the updating procedure was successful.
	 */
	public boolean update_supplier (Supplier supplier)
	{
		return db.update_supplier(supplier);
	}
	
	/**
	 * Method for finding a supplier by a given id and returning all his data as an Supplier Object.
	 *
	 * @param id A String value of a valid id of a supplier
	 * @return all the data of the  if such is found.
	 */
	public Supplier find_supplier_by_id(String id)
	{
		return db.find_supplier("id", Integer.parseInt(id));
	}
	
	/**
	 * Finds a supplier by his name and returns him. 
	 *
	 * @param name String value of the name of the desired supplier.
	 * @return the first supplier found in the database(if there is such).
	 */
	public Supplier find_supplier_by_name(String name)
	{
		return db.find_supplier("name", name);
	}
	
	/**
	 * Returns all the suppliers stored in the database - bothe deleted and non-deleted.
	 *
	 * @return A list with all the suppliers(if there are any).
	 */
	public List<Supplier> get_all_suppliers()
	{
		return db.multiple_find("", "", -1);
	}
	
	/**
	 * Provides all the suppliers, stored in the database, who have been deleted.
	 *
	 * @return a List with all deleted suppliers on user reuest. 
	 */
	public List<Supplier> get_deleted_suppliers()
	{
		return db.multiple_find("deleted", "", 1);
	}
	
	/**
	 * Provides all currently active (non-deleted) suppliers, that are used.
	 *
	 * @return a List with all non-deleted suppliers.
	 */
	public List<Supplier> get_non_deleted_suppliers()
	{
		return db.multiple_find("deleted", "", 0);
	}
	
	/**
	 * Method for restoring a supplier's state from deleted to non-deleted (active).
	 *
	 * @param id of the Supplier, who has been marked as deleted.
	 * @return a boolean statement whether the operation was successful.
	 */
	public boolean restore_supplier(String id)
	{
		Supplier suppl = find_supplier_by_id(id);
		suppl.setDeleted(false);
		return db.update_supplier(suppl);
	}
	
	/**
	 * Checks if a supplier with a given id exists in the system.
	 *
	 * @param id String value of the id of the supplier.
	 * @return a boolean statement that marks the .
	 */
	public boolean supplier_exists(String id)
	{
		return db.find_supplier("id", Integer.parseInt(id)) != null;
	}
	
	/**
	 * Finds a supplier by his id and marks him as deleted in the system
	 *
	 * @param id String value of the supplier's id.
	 * @return Boolean statement with the outcome of the operation.
	 */
	public boolean delete_supplier(String id)
	{
		Supplier suppl = this.find_supplier_by_id(id);
		suppl.setDeleted(true);
		return db.update_supplier(suppl);
	}
	
	public String find_city_by_zipcode(String zipcode)
	{
		return db.find_city(zipcode);
	}
	
	public boolean name_exists(String name)
	{
		return find_supplier_by_name(name) != null;
	}
}
